load('Result_2_Calibration.mat')
%% Derivative Matrix;
TempFun         =   @(Param)SubFun_Param2Mom(Info,Param);
[Mom,Result]    =   TempFun(ParamVec);
optset('parfdjac','tol',1e-2);
Jac_Mom2Param   =   parfdjac(TempFun,ParamVec);

%% Sensitivity
% Moment to Parameters
S_Mom2Param     =   Jac_Mom2Param./(abs(Mom(:))./abs(ParamVec(:)'));

% Parameters to Moments
WeightMat       =   diag(ones(length(Mom),1));
Jac_Param2Mom   =   -(Jac_Mom2Param'*WeightMat*Jac_Mom2Param)\(Jac_Mom2Param'*WeightMat);
S_Param2Mom     =   Jac_Param2Mom./(abs(ParamVec(:))./abs(Mom(:)'));

%% Figure
%--------------------------------------------------------------------------
% Setup
%--------------------------------------------------------------------------
List_Params     =   {'$\underline{b}$','$\bar{B}$'};
List_Moments    =   {'$\frac{Liquid\ Wealth}{Income}$, Median','MPC, Median'};
ColorMap_Param2Mom  = Graph_ColorMap(S_Param2Mom);
ColorMap_Mom2Param  = Graph_ColorMap(S_Mom2Param);


set(groot,'defaulttextinterpreter','Latex');  
set(groot, 'defaultAxesTickLabelInterpreter','Latex');  
set(groot, 'defaultLegendInterpreter','Latex'); 

fig_size    =   [1,1].*[1,0.25]*1/2;
fig_gap     =   [0.02,0.02];
fig_VMargin =   [0.20,0.02];
fig_HMargin =   [0.25,0.02]; 

fig_SubPlot =   @(ii_vv)subtightplot(1,1,ii_vv,fig_gap,fig_VMargin,fig_HMargin);


% Figure

Fig         =   FigureSetup('Parameters to Moments',fig_size);
ax          =   fig_SubPlot(1);
ZAxis   =   S_Param2Mom; ZAxis = [ZAxis;ZAxis(end,:)]; ZAxis = [ZAxis,ZAxis(:,end)];
XAxis   =   (0:2:2*(size(ZAxis,2)-1));
YAxis   =   (0:2:2*(size(ZAxis,1)-1));
ax = surf(XAxis,YAxis,ZAxis);
xlim([min(XAxis),max(XAxis)]);
ylim([min(YAxis),max(YAxis)]);
xticks((1:2:max(XAxis)-1));yticks((1:2:max(YAxis)-1));
xticklabels(List_Moments);yticklabels(List_Params);
colormap(ColorMap_Param2Mom);colorbar;
for ii=1:size(S_Param2Mom,2)
    for jj=1:size(S_Param2Mom,1)
        text(2*(ii-1)+1-0.5,2*(jj-1)+1,S_Param2Mom(jj,ii)+5,num2str(round(S_Param2Mom(jj,ii),2),'%.2g'));
    end
end
view(2);

print('-depsc','-r1000',Fig,'TableGraphs/Identification/Sensitivity_Param2Mom');
% close(Fig);

Fig         =   FigureSetup('Parameters to Moments',fig_size);
ax          =   fig_SubPlot(1);
ZAxis   =   S_Mom2Param; ZAxis = [ZAxis;ZAxis(end,:)]; ZAxis = [ZAxis,ZAxis(:,end)];
XAxis   =   (0:2:2*(size(ZAxis,2)-1));
YAxis   =   (0:2:2*(size(ZAxis,1)-1));
ax = surf(XAxis,YAxis,ZAxis);
xlim([min(XAxis),max(XAxis)]);
ylim([min(YAxis),max(YAxis)]);
xticks((1:2:max(XAxis)-1));yticks((1:2:max(YAxis)-1));
yticklabels(List_Moments);xticklabels(List_Params);
colormap(ColorMap_Mom2Param);colorbar;
for ii=1:size(S_Mom2Param,2)
    for jj=1:size(S_Mom2Param,1)
        text(2*(ii-1)+1-0.5,2*(jj-1)+1,S_Mom2Param(jj,ii)+5,num2str(round(S_Mom2Param(jj,ii),2),'%.2g'));
    end
end
view(2);

print('-depsc','-r1000',Fig,'TableGraphs/Identification/Sensitivity_Mom2Param');
% close(Fig);